<?php
class Countries extends MY_Controller
{
	var $curl = 'countries';
	var $name = '';
	var $search = '';
	var $language;
	var $submenu = "_menu";	
	var $id_country = '';
	
	function __construct()
	{
		parent::__construct();
				
		$this->load->model('/public/geo_country_model');

		$this->validation->set_fields($this->geo_country_model->fields);

		$this->data['Title'] = T_('Страны / Редактировать');

		$this->language = parent::getLang();
		
		$this->name = T_('Страны');		
	}
	
	function index()
	{
		$this->geo_country_model->search();
		$this->search = $this->data['search'];
		//echo $this->router->show_404();
		/*$CI =& get_instance();
		$CI->output->set_status_header('404');
		$CI->router->show_404();*/
		
		$this->renderLayout();
	}

	function create()
	{
		$this->data['Title'] = $this->name . ' / ' . T_('Create');
		if (count($_POST))
		{						
			$rules = array('name', 'iso_nr', 'alpha2', 'alpha3', 'phone_code');
			$this->validation->set_rules($this->geo_country_model->getRules($rules));

			$this->validation->set_message('__nameUnique', '<span class="black bold">«Страна»</span> с таким названием уже существует! Измените название.');

			if ($this->validation->run() === TRUE)
			{
				$item['name'] 	= $this->validation->name;
				$item['iso_nr'] = (!empty($this->validation->iso_nr)) ? $this->validation->iso_nr : null;
				$item['alpha2'] = $this->validation->alpha2;
				$item['alpha3'] = $this->validation->alpha3;
				$item['phone_code'] = $this->validation->phone_code;												
				
				if ($this->geo_country_model->insert($item))
				{
					redirect($this->curl . '/index');	
				}				
			}
		}

		$this->renderLayout();		
	}
	
	function edit($id = null)
	{
		if (!empty($id))
		{			
			$object = $this->geo_country_model->get($id);
			$this->data['Title'] = $object['name'] . ' / ' . T_('Edit');
					
			if (!count($object))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
	
				return false; 
			}
		
			if (!count($_POST))
			{
				$this->validation->id 		= $object['id']; 
				$this->validation->name 	= $object['name'];
				$this->validation->iso_nr 	= $object['iso_nr'];
				$this->validation->alpha2 	= $object['alpha2'];
				$this->validation->alpha3 	= $object['alpha3'];
				$this->validation->phone_code = $object['phone_code'];
				$this->form = $this->curl . '/edit'; 
			}
			else
			{
				$this->id_country = $id;
				
				$rules = array('name', 'iso_nr', 'alpha2', 'alpha3', 'phone_code');
				$this->validation->set_rules($this->geo_country_model->getRules($rules));
	
				$this->validation->set_message('__nameUnique', '<span class="black bold">«Страна»</span> с таким названием уже существует! Измените название.');
				
				if ($this->validation->run() === TRUE)
				{
					$item['id'] 	= (int)$this->validation->id;
					$item['name'] 	= $this->validation->name;
					$item['iso_nr'] = (int)$this->validation->iso_nr;
					$item['alpha2'] = $this->validation->alpha2;
					$item['alpha3'] = $this->validation->alpha3;
					$item['phone_code'] = $this->validation->phone_code;
					
					$this->geo_country_model->update($item);

					redirect($this->curl . '/index');
				}
			}			
		}
		
		$this->renderLayout();
	}
	
	function deleteItems()
	{
		$data = $this->geo_country_model->delete($_REQUEST['items']);

		echo $data;
	}

	function delete($id)
	{
		$data = $this->geo_country_model->delete($id);

		if ($data)
		{
			redirect($this->curl . '/index');	
		}
		else
		{
			redirect($this->curl . '/edit/'. $id);
		}
	}
		
	function __nameUnique($name)
	{		
		return $this->geo_country_model->nameUnique($name);
	}	
}
?>